#!/usr/bin/env python

import socket
import sys
import array
##test = chr.array[0x01, 0x02, 0x03]
byte0 = chr(0xFF)
byte1 = chr(0x00)
byte2 = chr(0x00)
byte3 = chr(0x19)
byte4 = chr(0x05)
byte5 = chr(0x64)
#test6 = array.array(c, [0xff, 0x00, 0x05, 0x19, 0x32, 0x0d, 0x05, 0x32, 0x14, 0x05])
tes6 = [chr(0xff), chr(0x00), chr(0x19), chr(0x0a), chr(0x00), chr(0x14), chr(0x05), chr(0x20), chr(0x10), chr(0x14)]

test5 = [chr(0xff), chr(0x00), chr(0x32), chr(0x14), chr(0x0a), chr(0x32), chr(0x0d), chr(0x05)]
#Global Variables
angle = int(20)
fwd = chr(0x05)
rev = chr(0x0a)
size = 1024
id1 = chr(0x19)
id2 = chr(0x14)
id3 = chr(0x0d)
drct1 = chr(0x0a)
drct2 = chr(0x05)
drct3 = chr(0x05)
m1 = ''
m2 = ''
m3 = ''

test7 = [chr(0xff), chr(0x00), id1, drct1, m1, id2, drct2, m2, id3, drct3, m3]

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

def alert(msg):
	print >>sys.stderr, msg
	sys.exit(1)

def wButton():
	if m1 <= 100:
		m1 += int(5)
	if m3 >= -100:
		m3 -= int(5)
def eButton():
	if m2 <= 100:
		m2 += int(5)
	if m3 >= -100:
		m3 -= int(5)
def fulfwd():
	m1 = int(100)
	m2 = int(100)
	m3 = int(0)

def stp():
	m1 = int(0)
	m2 = int(0)
	m3 = int(0)

def testmove2():
	global m1
	m1 = chr(0x20)
	global m2
	m2  = chr(0x32)
	global m3
	m3 = chr(0x25)
	transmit = ''
	for x in test7:
		transmit = transmit + x
	s.send(transmit)
	
def testmove():
	test4 = ''
	for x in test5:
		test4 = test4 + x
	"""
	m1 = int(40)
	s.send(byte0)
	s.send(byte1)
	s.send(id1)
	s.send(fwd)
	s.send(m1)
	s.send(id2)
	s.send(fwd)
	s.send(m1)
	s.send(id3)
	s.send(fwd)
	#s.send(m1)
	"""
	s.send(test4)

def connectSvr():
	##host = raw_input('\nPlease enter the host address: ')
	host = '192.168.1.138'
	port = 50000
	size = 100

	try:
		s.connect((host,port))
		print '\nConnecting to server...\n'
	except Exception, e:
		alert('Something is wrong with %s:%d. Exception type is %s' % (host, port, e))
	print 'Connection Established!\n'

def txBender(tdata):
	print 'Sending: %s\n' % (tdata)
##	byte6 = byte1 - chr(0x01)
##	test2 = chr(test1)
##	s.send(test2)
	s.send(byte0)
	s.send(byte1)
	s.send(m1)
	s.send(byte3)
	s.send(byte4)
	s.send(byte5)
	s.send(byte6)
	s.send(byte7)
	s.send(byte8)
	s.send(byte9)
	rdata = s.recv(size)
	print '\nReceiving: %s\n' % (rdata) 
		

running = 1

connectSvr()
while running:
	choice = raw_input('What would you like to do?\n1. Send Data\n2. Exit\n')
	if choice == '2':
		running = 0	
	else:
		tdata = raw_input('\nEnter data to send to the robot:\n')
	#txBender(byte1)
		testmove2()

s.send('stop')
s.close()
exit()
